package dungnt.vpoint.mathtest.dialog;

import java.util.ArrayList;
import java.util.List;

import dungnt.vpoint.mathtest.GameMode;
import dungnt.vpoint.mathtest.PlayActivity;
import dungnt.vpoint.mathtest.R;
import dungnt.vpoint.mathtest.db.Top50Adapter;
import dungnt.vpoint.mathtest.db.ScoreDatabase;
import dungnt.vpoint.mathtest.db.User;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class Top50Dialog extends Activity {
	int temp;
	int a = 0;
	boolean ischeck = true;
	TextView lblName;
	TextView lblScore;
	TextView lblTime;
	TextView lblTop50Message;
	Button btnTop50Ok;
	ScoreDatabase scoreData;
	GameMode gameMode;
	int tg;
	int bg;
	List<User> array;
	private static ListView lv;
	public static Context mContext;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.top50_highscore);
		
		mContext = this;
		scoreData = new ScoreDatabase(this);
		scoreData.openDB();
		array = new ArrayList<User>();
		
		btnTop50Ok = (Button) findViewById(R.id.btnTop50Ok);
		btnTop50Ok.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Intent i = new Intent(Top50Dialog.this, PlayActivity.class);
				startActivity(i);
				finish();
			}
		});
		
		gameMode = new GameMode();
		
		lblTop50Message = (TextView) findViewById(R.id.top50_message);
		Typeface messageFont = Typeface.createFromAsset(Top50Dialog.mContext.getAssets(), "MYRIADPRO-REGULAR.OTF");
		lblTop50Message.setTypeface(messageFont);
		
		lv = (ListView) findViewById(R.id.lv_top50);
		lv.setCacheColorHint(Color.TRANSPARENT);
		
		Cursor cursor = scoreData.getHighScores();
		if (cursor.moveToFirst()) {

			do {
				a++;
				array.add(new User(cursor.getString(2), cursor.getInt(3),cursor.getString(4)));
				if (a < 49) {
					temp = cursor.getInt(3);
					ischeck = true;

				} else {
					ischeck = false;
					temp = cursor.getInt(3);
				}
			} while (cursor.moveToNext() && ischeck);

		}
		scoreData.closeDB();
		if (temp < GameMode.topScore)
			lblTop50Message.setText("Congratulations! You are in Top 50 best players!");
		else
			lblTop50Message.setText("You are in the x of y. Try harder to be on the world top!");
		Top50Adapter adapter = new Top50Adapter(getBaseContext(), array);
		lv.setAdapter(adapter);

	}

}
